package com.gaodun.hk.common.response;

import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * @author fengyamin
 * @date 2020/04/14
 **/
@Data
public class ServiceException extends RuntimeException {
    protected HttpStatus httpStatus;
    private int errorCode;
    private String detail;
    private Object data;

    protected ServiceException(IStatusCode statusCode) {
        super(statusCode.getMessage());
        this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        this.errorCode = statusCode.getCode();
    }

    protected ServiceException(IStatusCode statusCode, Object data) {
        super(statusCode.getMessage());
        this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        this.errorCode = statusCode.getCode();
        this.data = data;
    }

    protected ServiceException(IStatusCode statusCode, String detail) {
        super(detail);
        this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        this.errorCode = statusCode.getCode();
    }

    protected ServiceException(IStatusCode statusCode, Throwable cause) {
        super(statusCode.getMessage(), cause);
        this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        this.errorCode = statusCode.getCode();
        this.detail = cause.getMessage();
    }

    public static ServiceException newServiceException(IStatusCode statusCode) {
        return new ServiceException(statusCode);
    }

    public static ServiceException newServiceException(IStatusCode statusCode, Object data) {
        return new ServiceException(statusCode, data);
    }

    public static ServiceException newInternalServiceException(String detail) {
        return new ServiceException(StatusCode.INTERNAL_SERVICE_ERROR, detail);
    }
}
